(function($) {
	var dictTable,searchBtn,addBtn,dictModel,dictModelOkBtn,dictModelFormControl,
	dictGroupParam,isDelParam,
	id,dictGroup,dictDescr,dictItemKey,dictItemValue,
	dict={};
	
	dict.init=function(){
		dictGroupParam= $("#dictGroup"),
		isDelParam=$("#isDel"),
		searchBtn = $('#searchBtn'),
		addBtn = $('#addBtn'),
		
		dictModel= $('#dictModel'),
		dictModelOkBtn=$('#dictModel  .ok'),
		dictModelFormControl= $('#dictModel  .form-control'),
		id=$('#dictModel [name="id"]'),
		dictGroup=$('#dictModel [name="dictGroup"]'),
		dictDescr=$('#dictModel [name="dictDescr"]'),
		dictItemKey=$('#dictModel [name="dictItemKey"]'),
		dictItemValue=$('#dictModel [name="dictItemValue"]'),
		
		dictTable = $("#dictTable").dataTable({
			"destroy": true,    //初始化表格
			"searching":false,//搜索框
			"deferRender" : true,
			"processing" : true,
			"serverSide" : true,
			"aLengthMenu": [10,20,30,50,100,500],  //更改显示记录数选项
			"ajax" : {
				// url可以直接指定远程的json文件，或是MVC的请求地址 /Controller/Action
				url : base_url + "/dict/pageList",
				type : 'POST',
				// 传给服务器的数据，可以添加我们自己的查询参数
				data : function(param) {
					var obj ={};
					obj.dictGroup = dictGroupParam.val();
					obj.isDel = isDelParam.val();
					obj.start = param.start;
		        	obj.length = param.length;
					return obj;
				}
			},
			"language" : {
				"sProcessing" : "处理中..." ,
				"sLengthMenu" : "每页 _MENU_ 条记录" ,
				"sZeroRecords" : "没有匹配结果" ,
				"sInfo" : "第 _PAGE_ 页 ( 总共 _PAGES_ 页，_TOTAL_ 条记录 )" ,
				"sInfoEmpty" : "无记录" ,
				"sInfoFiltered" : "(由 _MAX_ 项结果过滤)",
				"sInfoPostFix" : "",
				"sSearch" : "搜索" ,
				"sUrl" : "",
				"sEmptyTable" : "表中数据为空" ,
				"sLoadingRecords" : "载入中..." ,
				"sInfoThousands" : ",",
				"oPaginate" : {
					"sFirst" : "首页",
					"sPrevious" : "上一页" ,
					"sNext" : "下一页" ,
					"sLast" : "末页"
				},
				"oAria" : {
					"sSortAscending" : ": 以升序排列此列" ,
					"sSortDescending" : ": 以降序排列此列"
				}
			},
			"columns" : [ 
				{
					"data" : 'id',
					"bSortable" : false,
					"visible" : false
				},
				{
					"title" : '字典组编码',
					"data" : 'dictGroup',
					"bSortable" : false,
					"visible" : true,
					"width" : '10%'
				},
				{
					"title" : '字典组描述',
					"data" : 'dictDescr',
					"bSortable" : false,
					"visible" : true,
					"width" : '20%'
				},
				{
					"title" : '字典项编码',
					"data" : 'dictItemKey',
					"bSortable" : false,
					"visible" : true,
					"width" : '10%'
				},
				{
					"title" : '字典项内容',
					"data" : 'dictItemValue',
					"bSortable" : false,
					"visible" : true,
					"width" : '10%'
				},
				{
					"title" : '状态',
					"data" : 'isDel',
					"bSortable" : false,
					"visible" : true,
					"width" : '10%',
					"render": function ( data, type, row , meta) {
						
						if(1 == data){
							return "禁用";
						}
						
						if(0 == data){
							return "启用";
						}
						
						return data;
					}
				},
				{
					"title" : '操作',
					"data" : null,
					"bSortable" : false,
					"visible" : true,
					"width" : '10%',
					"render": function ( data, type, row , meta) {
						
						var html = '<p>';
						if(1 == row.isDel){
							html += '<button class="btn btn-primary btn-xs change" type="button">启用</button>';
						}
						
						if(0 == row.isDel){
							html += '<button class="btn btn-primary btn-xs change" type="button">禁用</button>';
						}
						html += '<button class="btn btn-warning btn-xs update" type="button">编辑</button>';
						html += '</p>';
						return html;
					}
				}
			]

		});
		
		
		
		
		
		
		dict.bindEvent();
	};
	
	
	dict.bindEvent = function(){
		searchBtn.on('click', function(){
			dictTable.fnDraw();
		});
		
		//新增
		addBtn.on('click', function(){
			dictModelFormControl.val('');
			dictModel.modal('show');
		});
		
		//编辑
		dictTable.on('click','.btn.update', function(){
			dictModelFormControl.val('');
			
			var tr = $(this).closest("tr")[0];
			var data = dictTable.fnGetData(tr);
			id.val(data.id);
			dictDescr.val(data.dictDescr);
			dictGroup.val(data.dictGroup);
			dictItemKey.val(data.dictItemKey); 
			dictItemValue.val(data.dictItemValue);
			
			
			dictModel.modal('show');
		});
		
		//保存
		dictModelOkBtn.on('click', function(){
			$.ajax({
				type : 'POST',
				url : base_url+"/dict/addOrUpdate",
				data : {
					"id" : $('#dictModel [name="id"]').val(),
					"dictDescr" : $('#dictModel [name="dictDescr"]').val(),
					"dictGroup" : $('#dictModel [name="dictGroup"]').val(),
					"dictItemKey" : $('#dictModel [name="dictItemKey"]').val(),
					"dictItemValue" : $('#dictModel [name="dictItemValue"]').val()
				},
				dataType : "json",
				success : function(data){
					if (data.code == 200) {
						dictModel.modal('hide');
						dictTable.fnDraw('page');
					} else {
						alert(data.msg)
					}
				},
			});
		});
		
		//启用禁用
		dictTable.on('click','.btn.change', function(){
			var tr = $(this).closest("tr")[0];
			var data = dictTable.fnGetData(tr);
			
			$.ajax({
				type : 'POST',
				url : base_url+"/dict/addOrUpdate",
				data : {
					"id" : data.id,
					"isDel" : 1 == data.isDel? 0:1
				},
				dataType : "json",
				success : function(data){
					if (data.code == 200) {
						dictTable.fnDraw('page');
					} else {
						alert(data.msg)
					}
				},
			});
		});
		
	};
	
	
	window.dict = dict;
	$(function(){
		dict.init();
	});
})(jQuery);